package com.hunan.chinamobile.onenet;

import com.hunan.chinamobile.constant.SysConstant;
import com.hunan.chinamobile.onenet.adpat.CreateDeviceOpe;
import com.hunan.chinamobile.onenet.entity.Device;
import com.hunan.chinamobile.onenet.http.HttpSendCenter;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OneNetBuilder {

    static Logger logger = LoggerFactory.getLogger(OneNetBuilder.class);

    public static String createDevice(String deviceName,String imei,String imsi){
        CreateDeviceOpe deviceOpe = new CreateDeviceOpe(SysConstant.ONE_NET_APPKEY);
        Device device = new Device(deviceName, imei, imsi);
        JSONObject json = deviceOpe.operation(device, device.toJsonObject());
        logger.info("==> created device result : "+json.toString());
        int errno = json.getInt("errno");
        if(errno == 0){
            return json.getJSONObject("data").getString("device_id");
        }else{
            return null;
        }
    }

    public static int delDevice(String deviceId){
        JSONObject json = HttpSendCenter.delete(SysConstant.ONE_NET_APPKEY,new Device().toUrl()+"/"+deviceId);
        logger.info("==> delete device result : "+json.toString());
        return json.getInt("errno") == 0 ? 1 : -1;
    }
}
